package com.pshcp.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.SecurityUtil;
import com.pshcp.ao.security.UpdatePasswordAO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.service.DoctorService;
import com.pshcp.service.PatientUserService;
import com.pshcp.tool.SecurityTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 通用接口-密码
 * @Author: xiaoche
 * @Date: 2022/11/4 01:41
 */
@Api(tags = {"通用接口-密码"})
@Slf4j
@RestController
@RequestMapping("/api/security")
public class SecurityAPI {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientUserService patientUserService;

    @ApiOperation("APP医生端修改密码")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "APP医生端修改密码")
    @PostMapping("updateDoctorPassword")
    public ResultDTO<String> updateDoctorPassword(@RequestBody UpdatePasswordAO updatePasswordAO) throws Exception {
        SecurityTool.checkUpdatePassword(updatePasswordAO);
        Long userId = ReqComTool.getUserId();
        Doctor doctor = doctorService.findById(userId);
        CommonTool.checkObj(doctor, "90000007");
        if (!doctor.getPassword().equals(SecurityUtil.MD5(updatePasswordAO.getOldPassword()))) {
            return ResultBuilder.failure("10090003");
        }
        Doctor updateDoctorPassword = SecurityTool.buildUpdateDoctorPassword(doctor, updatePasswordAO);
        doctorService.update(updateDoctorPassword);
        return ResultBuilder.success(null);
    }

    @ApiOperation("APP患者端修改密码")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "APP患者端修改密码")
    @PostMapping("updatePatientPassword")
    public ResultDTO<String> updatePatientPassword(@RequestBody UpdatePasswordAO updatePasswordAO) throws Exception {
        SecurityTool.checkUpdatePassword(updatePasswordAO);
        Long userId = ReqComTool.getUserId();
        PatientUser patientUser = patientUserService.findById(userId);
        CommonTool.checkObj(patientUser, "90000007");
        if (!patientUser.getPassword().equals(SecurityUtil.MD5(updatePasswordAO.getOldPassword()))) {
            return ResultBuilder.failure("10090004");
        }
        PatientUser updatePatientPassword = SecurityTool.buildUpdatePatientPassword(patientUser, updatePasswordAO);
        patientUserService.update(updatePatientPassword);
        return ResultBuilder.success(null);
    }
}
